<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
<h1>Onderdeel-probleem aanpassen</h1>
</hgroup>

<?php if(!isset($_POST['Submit'])) { 
    $query="SELECT * FROM itemproblem WHERE Id='" .$_GET["id"]."'";
    $result = mysql_query($query) or die ("FOUT1: " . mysql_error());

    while ($rij = mysql_fetch_array($result)){
        $ItemId = $rij['ItemId'];
        $ParentId = $rij['ParentId'];
        $Name = $rij['Name'];
        $Value= $rij['Value'];
        $Activated = $rij['Activated'];
    }
?>
<form  name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
	<tr>
            <td>Kamer:</td>
            <td>
                <?php
                    $sql1 = "SELECT * from roomitem WHERE Id=".$ItemId."";
                    $result1 = mysql_query($sql1) or die (mysql_error());
                    while($row1 = mysql_fetch_array($result1))
                    {
                        $sql2 = "SELECT * from roomtype WHERE Id=".$row1['RoomId']."";
                        $result2 = mysql_query($sql2) or die (mysql_error());
                        while($row2 = mysql_fetch_array($result2))
                        {
                            echo "<input name=\"kamer\" value=\"".$row2['Name']."\" disabled type=\"text\">";
                        }
                    }
                ?>
            </td>
	</tr>
        <tr>
            <td>Onderdeel:</td>
            <td>
                <?php
                $sql3 = "SELECT * from roomitem WHERE Id=".$ItemId."";
                $result3 = mysql_query($sql3) or die (mysql_error());
                while($row3 = mysql_fetch_array($result3))
                {
                    echo "<input name=\"onderdeel\" value=\"".$row3['Name']."\" disabled type=\"text\">";
                }
            ?>
            </td>
	</tr>
        <tr>
            <td>Parent:</td>
            <td>
                <?php
                    if ($ParentId == -1)
                    {
                        echo "<input name=\"parent\" value=\"Geen parent\" disabled type=\"text\">";
                    }
                    else
                    {
                        $sql4 = "SELECT * from itemproblem WHERE Id=".$ParentId."";
                        $result4 = mysql_query($sql4) or die (mysql_error());
                        while($row4 = mysql_fetch_array($result4))
                        {
                            echo "<input name=\"parent\" value=\"".$row4['Name']."\" disabled type=\"text\">";
                        }
                    }
                ?>
            </td>
        </tr>
        <tr>
            <td>Onderdeel-probleem:</td>
            <td><input required="required" value="<?php echo $Name; ?>" name="probleem" type="text"></td>
        </tr>
        <tr>
            <td>Extra tekst kunnen ingeven:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="extratekst" class="onoffswitch-checkbox" id="extratekst" <?php if ($Value == "T") { echo "checked"; } ?>>
                    <label class="onoffswitch-label" for="extratekst">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
        <tr>
            <td>Geactiveerd:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="activatie" class="onoffswitch-checkbox" id="activatie" <?php if ($Activated == "T") { echo "checked"; } ?>>
                    <label class="onoffswitch-label" for="activatie">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
 	<tr>
            <td><input type="hidden" name="id" value="<?php echo($_GET["id"]);?>" />
                <input type="hidden" name="kamer" value="<?php echo($_GET["kamer"]);?>" />
                <input type="hidden" name="onderdeel" value="<?php echo($_GET["onderdeel"]);?>" />
                <input type="submit" name="Submit" value="Opslaan">
            </td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
<?php 
} 
else 
{	
	$error = array();
        
        if($_POST['probleem'] == "") {
		$error[] = 'Er is geen probleem ingevuld.';
	}
        
        if(isset($_POST['extratekst'])) {
            $extratekst = "T";
	}
        else
        {
            $extratekst = "F";
        }
        
        if(isset($_POST['activatie'])) {
            $activatie = "T";
	}
        else
        {
            $activatie = "F";
        }
        
        $errors = sizeof($error); // aantal errors tellen
        if($errors != 0) { // Er is minstens 1 error
                echo "<span style=\"color:red\">Dit onderdeel kan niet worden aangepast omwille van de volgende reden(en):</span>";
                echo "<ul>";
                        for($i = 0; $i < $errors; $i++) {
                                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                        }
                echo "</ul>";
        } 
        else 
        {
            $query = "UPDATE itemproblem SET Name='$_POST[probleem]', Activated='$activatie', Value='$extratekst' WHERE Id = '$_POST[id]'";
            mysql_query($query) or die ("FOUT2: " . mysql_error());
            echo "Het onderdeel-probleem is aangepast.<br>";
            echo "Klik <a href=\"onderdeelprobleem.php?kamer=".$_POST['kamer']."&onderdeel=".$_POST['onderdeel']."\">hier</a> om terug te gaan naar het overzicht.";
        }
} 
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>